//
//  MessageDetailViewController.m
//  huashiedu
//
//  Created by oceans on 2018/9/12.
//  Copyright © 2018年 hema. All rights reserved.
//

#import "MessageDetailViewController.h"
#import "ConversationSettingViewController.h"
#import "TipManager.h"

@interface MessageDetailViewController () <RCChatSessionInputBarControlDelegate, UINavigationControllerDelegate, UIImagePickerControllerDelegate>

@end

@implementation MessageDetailViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    self.navigationItem.backBarButtonItem = [[UIBarButtonItem alloc] initWithTitle:@"" style:UIBarButtonItemStylePlain target:nil action:nil];
    [self setupView];
}

- (void)viewWillAppear:(BOOL)animated {
    [super viewWillAppear:animated];
    self.title = [NSString stringWithFormat:@"%@（共%d人）", self.messageModel.name, self.messageModel.nums];
}

- (void)setupView {
    UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
    button.frame = CGRectMake(0, 0, 44, 44);
    [button setImage:[UIImage imageNamed:@"conversationSetting"] forState:UIControlStateNormal];
    [button setImageEdgeInsets:UIEdgeInsetsMake(10, 20, 10, 0)];
    [button addTarget:self action:@selector(showConversationSetting) forControlEvents:UIControlEventTouchUpInside];
    
    UIView *view = [[UIView alloc] initWithFrame:button.bounds];
    [view addSubview:button];
    self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc] initWithCustomView:view];
}

- (void)showConversationSetting {
    ConversationSettingViewController *setting = [[ConversationSettingViewController alloc] init];
    setting.groupId = self.targetId;
    setting.messageModel = self.messageModel;
    [self.navigationController pushViewController:setting animated:YES];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
    
}

//融云的拍照界面无法正常返回
- (void)pluginBoardView:(RCPluginBoardView *)pluginBoardView clickedItemWithTag:(NSInteger)tag {
    
    if (tag == 1002) {
        if ([UIImagePickerController isSourceTypeAvailable:UIImagePickerControllerSourceTypeCamera]) {
            UIImagePickerController *controller = [[UIImagePickerController alloc] init];
            controller.sourceType = UIImagePickerControllerSourceTypeCamera;
            controller.delegate = self;
            [self presentViewController:controller animated:YES completion:nil];
        }else {
            [TipManager showTip:@"无法打开相机"];
        }
        
    }else {
        [super pluginBoardView:pluginBoardView clickedItemWithTag:tag];
        [[UIBarButtonItem appearance] setTitleTextAttributes:@{NSForegroundColorAttributeName: [UIColor colorWithRed:63/255.0 green:63/255.0 blue:63/255.0 alpha:1.0]} forState:UIControlStateNormal];
    }
}

- (void)imagePickerControllerDidCancel:(UIImagePickerController *)picker {
    [picker dismissViewControllerAnimated:YES completion:nil];
}

- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary<NSString *,id> *)info {
    [picker dismissViewControllerAnimated:YES completion:nil];
    UIImage *image = [info objectForKey:UIImagePickerControllerOriginalImage];
    NSData *data = UIImageJPEGRepresentation(image, 0.3);
    [self sendMessage:[RCImageMessage messageWithImageData:data] pushContent:nil];
    NSLog(@"%@", info);
}

@end
